using MicroMsg.Common.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Resources;
namespace MicroMsg.Manager
{
	public class VoiceEffectManager
	{
		private const string TAG = "VoiceEffectManager";
		private string voiceFileName;
		private float currentVolume = 1f;
		private AutoResetEvent autoResetEvent = new AutoResetEvent(false);
		private Thread soundThread;
		private VoicePlayStatus playerStatus;
		public bool IsLoudVoice = true;
		private SoundEffect mSoundEffect;
		private SoundEffectInstance mSoundEffectInstance;
		private bool isInitVoice;
		private MemoryStream stream;
		private bool isLoadFile;
		public VoiceEffectManager(string _voiceFileName, float forceVolume = 1f)
		{
			this.voiceFileName = _voiceFileName;
			this.currentVolume = forceVolume;
		}
		public void PlaySound()
		{
			if (this.soundThread == null)
			{
				this.soundThread = new Thread(new ThreadStart(this.PlayThreadProc));
				this.soundThread.Start();
			}
			if (this.playerStatus == VoicePlayStatus.Idle)
			{
				this.autoResetEvent.Set();
			}
		}
		private void PlayThreadProc()
		{
			while (true)
			{
				this.autoResetEvent.WaitOne();
				Log.smethod_5("VoiceEffectManager", "playerStatus = VoicePlayStatus.Playing");
				this.playerStatus = VoicePlayStatus.Playing;
				if (this.LoadVoiceFile())
				{
					this.InitVoice();
					if (this.IsLoudVoice)
					{
						this.mSoundEffectInstance.set_Volume(this.currentVolume);
						SoundEffect.set_MasterVolume(this.currentVolume);
					}
					else
					{
						this.mSoundEffectInstance.set_Volume(this.currentVolume * 0.7f);
						SoundEffect.set_MasterVolume(this.currentVolume * 0.7f);
					}
					this.mSoundEffectInstance.Play();
					do
					{
						try
						{
							FrameworkDispatcher.Update();
							goto IL_D0;
						}
						catch
						{
							goto IL_D0;
						}
						break;
						IL_D0:
						Thread.Sleep(33);
					}
					while (this.mSoundEffectInstance.get_State() == null);
					IL_9C:
					Log.smethod_5("VoiceEffectManager", "mSoundEffectInstance.State:" + this.mSoundEffectInstance.get_State());
					this.mSoundEffectInstance.Stop();
					goto IL_2A;
					goto IL_9C;
				}
				IL_2A:
				this.playerStatus = VoicePlayStatus.Idle;
			}
		}
		private void InitVoice()
		{
			if (this.isInitVoice)
			{
				return;
			}
			this.mSoundEffect = new SoundEffect(this.stream.ToArray(), 16000, 1);
			this.mSoundEffectInstance = this.mSoundEffect.CreateInstance();
			this.mSoundEffectInstance.set_Volume(1f);
			this.isInitVoice = true;
		}
		private bool LoadVoiceFile()
		{
			if (this.isLoadFile)
			{
				return true;
			}
			this.stream = new MemoryStream();
			try
			{
				StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri(this.voiceFileName, 2));
				int num = 4096;
				byte[] array = new byte[4096];
				int num2;
				while ((num2 = resourceStream.get_Stream().Read(array, 0, num)) > 0)
				{
					this.stream.Write(array, 0, num2);
				}
				if (this.stream.get_Length() <= 0L)
				{
					bool result = false;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_2("VoiceEffectManager", "LoadVoiceFile error" + ex.get_Message(), ex);
				bool result = false;
				return result;
			}
			this.isLoadFile = true;
			return true;
		}
	}
}
